//
//  YKPushBannerManager.m
//  YK_BaseTools
//
//  Created by edward on 2020/2/27.

#import "YKPushBannerManager.h"
#import "YKPushViewProtocal.h"
@interface YKPushBannerManager ()
@property (nonatomic, strong) NSMutableArray<id<YKPushViewProtocal>> *pushQueue;
@property (nonatomic, strong) id<YKPushViewProtocal> currentView;
@property (nonatomic, assign) BOOL isPushing;
@end

@implementation YKPushBannerManager

static YKPushBannerManager *_instance;

+ (id)allocWithZone:(struct _NSZone *)zone
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [super allocWithZone:zone];
    });

    return _instance;
}

+ (instancetype)sharedInstance
{
    if (_instance == nil) {
        _instance = [[YKPushBannerManager alloc] init];
    }

    return _instance;
}

// MARK:- 把自定义view添加到Push队列
+(void)addCustomViewIntoPushQueue:(id<YKPushViewProtocal>)customView {
    [self addCustomViewIntoPushQueue:customView canCoverMembers:YES];
}

+(void)addCustomViewIntoPushQueue:(id<YKPushViewProtocal>)customView canCoverMembers:(BOOL)canCoverMembers
{
    if (![YKPushBannerManager sharedInstance].isPushing) {
        [[YKPushBannerManager sharedInstance].pushQueue addObject:customView];
        [[YKPushBannerManager sharedInstance] pushNext];
    } else {
        
        if (canCoverMembers) {
            NSMutableArray *tmpArray = @[].mutableCopy;
            // 先移除掉所有同类的banner
            [[YKPushBannerManager sharedInstance].pushQueue enumerateObjectsUsingBlock:^(id<YKPushViewProtocal>  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj respondsToSelector:@selector(indentifier)]) {
                    
                    if (![obj.indentifier isEqualToString:customView.indentifier]) {
                        [tmpArray addObject:obj];
                    }
                }
            }];
            [tmpArray addObject:customView];
            [YKPushBannerManager sharedInstance].pushQueue = tmpArray;
            if ([YKPushBannerManager sharedInstance].pushQueue.firstObject == customView &&
                [[YKPushBannerManager sharedInstance].currentView.indentifier isEqualToString:customView.indentifier] &&
                [[YKPushBannerManager sharedInstance].currentView respondsToSelector:@selector(endPush)]) {
                // 下一个要展示的就是当前类型则直接展示
                [[YKPushBannerManager sharedInstance].currentView endPush];
            }
        }
    }
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.pushQueue = @[].mutableCopy;
    }
    return self;
}

// MARK:- 展示下一个
- (void)pushNext
{
    if (self.pushQueue.count != 0) {
        id<YKPushViewProtocal> customerView = self.pushQueue.firstObject;
        [self.pushQueue removeObjectAtIndex:0];
        self.currentView = customerView;
        NSString *indentifier = @"";
        if ([self.currentView respondsToSelector:@selector(indentifier)]) {
            indentifier = self.currentView.indentifier;
        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kPushShowNotification" object:@{@"showId":indentifier}];
        
        __weak typeof(self) weakself = self;
        [customerView showPushAnimationInView:UIApplication.sharedApplication.keyWindow pushFinishHandler:^{
            __strong typeof(weakself) strongself = weakself;
            strongself.currentView = nil;
            [strongself pushNext];
        }];
        self.isPushing = YES;
    } else {
        self.isPushing = NO;
    }
}

@end
